//
//  TabBarController.swift
//  HeadlineNews
//
//  Created by han on 2019/5/30.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit


class TabBarController: UITabBarController {
    
    var tabbarAnimation: AnimatedTransitioning?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tabbar = Tabbar()
        self.setValue(tabbar, forKey: "tabBar")
        self.delegate = self
        self.tabbarAnimation = AnimatedTransitioning()
//        self.tabbarAnimation?.transitionType = .tab(.fromRight)
        self.tabbarAnimation?.transitionType = .none
        addChildViewControllers()
        
    }
    // TabBarVC 在执行init方法时，由于当前View还没有创建，super.init的时候会强制先调用当前的viewDidLoad。
    
    private func addChildViewControllers() {
       
        let storyboard = UIStoryboard(storyboard: .Home)
        let home: HomeViewController = storyboard.instantiateViewController()
        addChildViewController(home, title: "首页", imageName: "home")
        addChildViewController(VideoViewController(), title: "视频", imageName: "video")
        addChildViewController(TaskViewController(), title: "任务", imageName: "weitoutiao")
        addChildViewController(MineViewController(), title: "我的", imageName: "mine")
        
    }
    
    private func addChildViewController(_ controller: UIViewController, title: String, imageName: String) {
        addDefaultChildViewController(controller, imageName)
//        if Theme.isNight() {
//            addNightChildViewController(controller, imageName)
//        } else {
//            addDefaultChildViewController(controller, imageName)
//        }
        controller.title = title
        let navigationController = NavigationController(rootViewController: controller)
        addChild(navigationController)
    }
    
    private func addDefaultChildViewController(_ controller: UIViewController, _ imageName: String) {
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_tabbar_press_night_32x32_")
        controller.tabBarItem.image = UIImage(named: imageName + "_tabbar_night_32x32_")
    }
    
    private func addNightChildViewController(_ controller: UIViewController, _ imageName: String) {
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_tabbar_press_32x32_")
        controller.tabBarItem.image = UIImage(named: imageName + "_tabbar_32x32_")
    }
}


extension TabBarController: UITabBarControllerDelegate {
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    
    // 不同tabbar的子VC转场动画
    func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        guard let transitionType = tabbarAnimation?.transitionType, case TransitionType.tab(_) = transitionType else { return nil }
        
        guard let fromIndex = tabBarController.viewControllers?.firstIndex(of: fromVC), let endIndex = tabBarController.viewControllers?.firstIndex(of: toVC) else { return nil }
        
        tabbarAnimation?.tabbarOpertaion = fromIndex < endIndex ?.leftToRight:.rightToLeft
        
        return tabbarAnimation
    }
}


